import requests
import logging
class Test_wdmovie:
    uid=""
    sid=""
    movieId=""  #电影id
    yyid=None #影院id
    orderId=None #订单id
    def setup_method(self):
        print("用例开始执行")
    def teardown_method(self):
        print("用例执行结束")
   #登录接口
    def test_login(self): #异常处理 try...except...finally

        try:
            url ="http://10.59.9.18/movieApi/user/v2/login"
            data ={"email":"1478997572@qq.com","pwd":"eWLPHopE945d2ivttHaQTQ=="}
            r = requests.post(url=url,data=data)
            print(r.json())
            logging.info(r.json())
            Test_wdmovie.uid=str(r.json()['result']['userId'])
            Test_wdmovie.sid=r.json()['result']['sessionId']
        except Exception as e:
            logging.info(e)
            logging.info("接口出错啦") #无所谓
        finally:  #不管代码有没有捕获到异常，都会执行
            print("接口执行结束！")
    # 查询即将上映电影列表
    def test_findComingSoonMovieList(self):
        try:
            url="http://10.59.9.18/movieApi/movie/v2/findComingSoonMovieList"
            data={"page":1,"count":10}
            headers={"userId":str(Test_wdmovie.uid),"sessionId":Test_wdmovie.sid}
            r = requests.get(url=url,params=data,headers=headers)
            print("查询即将上映电影列表:",r.json())
            logging.info(r.json())
            Test_wdmovie.movieId=r.json()['result'][0]['movieId']
            print(Test_wdmovie.movieId) #调试打印电影id
        except Exception as e:
            logging.info(e)
    #查看附近电影院
    def test_findNearbyCinemas(self):
        try:
            url="http://10.59.9.18/movieApi/cinema/v1/findNearbyCinemas"
            data={"longitude":"116","latitude":"40","page":1,"count":6}
            headers={"userId":str(Test_wdmovie.uid),"sessionId":Test_wdmovie.sid}
            r = requests.get(url=url,params=data,headers=headers)
            print("查看附近电影院:",r.json())
            logging.info(r.json())
            Test_wdmovie.yyid=r.json()['result'][0]['id'] #提取电影院id

        except Exception as e:
            logging.info(e)
    #根据电影ID和影院ID查询电影排期列表
    def test_findMovieSchedule(self):
        try:
            url="http://10.59.9.18/movieApi/movie/v2/findMovieSchedule"
            data={"movieId":Test_wdmovie.movieId,"cinemaId":Test_wdmovie.yyid} #电影院id关联， 电影id关联
            headers={"userId":str(Test_wdmovie.uid),"sessionId":Test_wdmovie.sid}  #关联使用uid，sid
            r = requests.get(url=url,params=data,headers=headers)
            print("根据电影ID和影院ID查询电影排期列表:",r.json())
            logging.info(r.json())
        except Exception as e:
            logging.info(e)

    #购票下单
    def test_buyMovieTickets(self): #异常处理 try...except...finally
        try:
            url ="http://10.59.9.18/movieApi/movie/v2/verify/buyMovieTickets"
            data ={"scheduleId":526,"seat":["1-1"],"sign":"dad198f0d48611985fb3b703f2659c48"}
            headers = {"userId": str(Test_wdmovie.uid), "sessionId": Test_wdmovie.sid}  # 关联使用uid，sid
            r = requests.post(url=url,json=data,headers=headers)
            print("购票下单:",r.json())
            logging.info(r.json())
            Test_wdmovie.orderId=r.json()['orderId'] #订单id提取

        except Exception as e:
            logging.info(e)
            logging.info("接口出错啦") #无所谓
        finally:  #不管代码有没有捕获到异常，都会执行
            print("接口执行结束！")
#支付
    def test_pay(self): #异常处理 try...except...finally
        try:
            url ="http://10.59.9.18/movieApi/movie/v2/verify/pay"
            data ={"payType":1,"orderId":Test_wdmovie.orderId}
            headers = {"userId": str(Test_wdmovie.uid), "sessionId": Test_wdmovie.sid}  # 关联使用uid，sid
            r = requests.post(url=url,json=data,headers=headers)
            print("支付:",r.json())
            logging.info(r.json())

        except Exception as e:
            logging.info(e)
            logging.info("接口出错啦") #无所谓
        finally:  #不管代码有没有捕获到异常，都会执行
            print("接口执行结束！")

        assert "支付异常" in r.text  #文本断言  后台支付接口未实现，因此暂时断言支付异常
        assert  r.json()['status'] == '1001'
        assert r.status_code == 200